/*
psp-gcc's library don't work well short wchar
*/

#include <stdio.h>

wchar_t* mywcscpy(wchar_t* dst, const wchar_t* src)
{
    while(*src)
    {
        *dst++ = *src++;
    }
    *dst = 0;
    return dst;
}

wchar_t* mywcscat(wchar_t* dst, const wchar_t* src)
{
    while(*dst)
    {
        dst++;
    }
    while(*src)
    {
        *dst++ = *src++;
    }
    *dst = 0;
    return dst;
}

wchar_t* mywcsstr(const wchar_t* str, const wchar_t* sub)
{
    wchar_t *cp = (wchar_t*)str;
    wchar_t *s1, *s2;

    if (!*sub)
    {
        return (wchar_t*)str;
    }
    while (*cp)
    {
        s1 = cp;
        s2 = (wchar_t*)sub;
        while (*s1 && *s2 && !(*s1-*s2))
        {
            s1++, s2++;
        }
        if (!*s2)
        {
            return(cp);
        }
        cp++;
    }
    return(NULL);
}

wchar_t* mywcschr(const wchar_t* str, wchar_t ch)
{
    while (*str && *str != ch)
    {
        str++;
    }
    if (*str == ch)
    {
        return ((wchar_t*)str);
    }
    return(NULL);
}

wchar_t* myitow(int val, wchar_t* str)
{
    wchar_t tmp[32];
    wchar_t* p = str;
    int k = 0;
    int i;

    do
    {
        tmp[k++] = val % 10;
        val /= 10;
    }
    while (val && k < 31);
    for (i = --k; i >= 0; i--)
    {
        *p++ = tmp[i] + '0';
    }
    *p = 0;
    return str;
}

wchar_t mytowlower(wchar_t wch)
{
    if (wch >= L'A' && wch <= L'Z')
    {
        return wch - L'A' + L'a';
    }
    return wch;
}

int mywcsnicmp(const wchar_t* first, const wchar_t* last, int count)
{
    wchar_t f,l;
    int result = 0;

    if(count)
    {
        do
        {
            f = mytowlower(*first);
            l = mytowlower(*last);
            first++;
            last++;
        } while ((--count) && f && (f == l));
        result = (int)(f - l);
    }
    return result;
}

int mywcsicmp(const wchar_t* first, const wchar_t* last)
{
    wchar_t f,l;
    int result = 0;

    do
    {
        f = mytowlower(*first);
        l = mytowlower(*last);
        first++;
        last++;
    } while (f && (f == l));
    result = (int)(f - l);
    return result;
}

int mywcslen(const wchar_t* str)
{
    int len = 0;
    while (*str)
    {
        len++;
        str++;
    }
    return len;
}
